package com.forum.module.system.controller.admin.user;

import cn.hutool.core.collection.CollUtil;
import com.forum.framework.common.enums.CommonStatusEnum;
import com.forum.framework.common.pojo.DataResult;
import com.forum.framework.common.pojo.PageResult;
import com.forum.framework.security.core.LoginUser;
import com.forum.framework.security.core.util.SecurityUtils;
import com.forum.module.system.controller.admin.user.vo.user.*;
import com.forum.module.system.convert.user.UserConvert;
import com.forum.module.system.dal.dataobject.user.UserDO;
import com.forum.module.system.service.user.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.forum.framework.common.pojo.DataResult.success;

/**
 * @author zihan.ouyang
 */
@Tag(name = "管理后台 - 用户")
@RestController
@RequestMapping("/admin/user")
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/create")
    @Operation(summary = "新增用户")
    public DataResult<Long> createUser(@RequestBody UserSaveReqVO reqVO) {
        Long id = userService.createUser(reqVO);
        return success(id);
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户信息")
    public DataResult<Boolean> updateUser(@RequestBody UserSaveReqVO reqVO) {
        userService.updateUser(reqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户信息")
    @Parameter(name = "id", description = "用户编号", required = true, example = "1024")
    public DataResult<Boolean> deleteUser(@RequestParam("id") Long id) {
        userService.deleteById(id);
        return success(true);
    }

    @PutMapping("/update-password")
    @Operation(summary = "修改用户密码")
    public DataResult<Boolean> updateUserPassword(@Valid @RequestBody UserUpdatePasswordReqVO reqVO, HttpServletRequest request) {
        userService.updateUserPassword(reqVO, request);
        return success(true);
    }

    @PutMapping("/forgot-password")
    @Operation(summary = "修改用户密码")
    public DataResult<Boolean> forgotUserPassword(@Valid @RequestBody ForgotPasswordReqVO reqVO, HttpServletRequest request) {
        userService.forgotUserPassword(reqVO, request);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "修改用户状态")
    public DataResult<Boolean> updateUserStatus(@Valid @RequestBody UserUpdateStatusReqVO reqVO) {
        userService.updateUserStatus(reqVO.getId(), reqVO.getStatus());
        return success(true);
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询用户列表")
    public DataResult<PageResult<UserRespVO>> page(@RequestBody @Valid UserPageReqVO pageReqVO) {
        // 获得用户分页列表
        PageResult<UserDO> pageResult = userService.getUserPage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal()));
        }

        return success(new PageResult<>(UserConvert.INSTANCE.covertList(pageResult.getList()), pageResult.getTotal()));
    }

    @GetMapping({"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取用户精简信息列表", description = "只包含被开启的用户，主要用于前端的下拉选项")
    public DataResult<List<UserSimpleRespVO>> getSimpleUserList() {
        // 获用户列表，只要开启状态的
        List<UserDO> list = userService.getUserListByStatus(CommonStatusEnum.NORMAL.getStatus());
        // 排序后，返回给前端
        return success(UserConvert.INSTANCE.covertSimpleList(list));
    }

    @GetMapping("/get")
    @Operation(summary = "获取用户信息详情")
    @Parameter(name = "id", description = "用户编号", required = true, example = "1024")
    public DataResult<UserRespVO> userInfo(@RequestParam("id") Long id) {
        UserDO user = userService.getUser(id);
        if (user == null) {
            return success(null);
        }
        return success(UserConvert.INSTANCE.covert(user));
    }

    @GetMapping("/getInfo")
    @Operation(summary = "获取当前登录用户详情")
    public DataResult<UserRespVO> getUserInfo() {
        Long loginUserId = SecurityUtils.getLoginUserId();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUserId == null) {
            return success(null);
        }
        UserDO user = userService.getUser(loginUserId);
        UserRespVO userRespVO = UserConvert.INSTANCE.covert(user);
        assert loginUser != null;
        userRespVO.setRole(loginUser.getRoles().get(0));
        return success(userRespVO);
    }
}
